package com.yunysha.controller;

import java.util.ArrayList;
import java.util.HashMap;

import net.sf.json.JSONArray;

import com.yunysha.DBManager.DBManager;
import com.yunysha.beans.ModuleBean;
import com.yunysha.beans.UserBean;
import com.yunysha.contentmodel.YunyshaCM;
import com.yunysha.struts.actionform.UserMasterForm;

public class YunyshaController {

	public UserBean validateUserLogin(String pUserId, String pUserPwd){
		
		DBManager dm;
		dm = DBManager.init();
		UserBean uBean = null;
		try{		
			
			dm.doConnect();
			
			YunyshaCM yCM = new YunyshaCM(dm);
			uBean = yCM.validateUserLoginCM(pUserId, pUserPwd);
			
		} catch(Exception ex){
			ex.printStackTrace();
		} finally{
			dm.doDisconnect();
		}
			
		return uBean;
	}
	
	public ArrayList<ModuleBean> retreiveModuleList(int pUserId){
		
		DBManager dm;
		dm = DBManager.init();
		ArrayList<ModuleBean> moduleList = null;
		try{		
			
			dm.doConnect();
			
			YunyshaCM yCM = new YunyshaCM(dm);
			moduleList = yCM.retreiveModuleListCM(pUserId);
			
		} catch(Exception ex){
			ex.printStackTrace();
		} finally{
			dm.doDisconnect();
		}
			
		return moduleList;
	}
	
	public void retreiveUserDetails(String pUserId, UserMasterForm pUserForm){
		
		DBManager dm;
		dm = DBManager.init();
		try{		
			
			dm.doConnect();
			
			YunyshaCM yCM = new YunyshaCM(dm);
			yCM.retreiveUserDetailsCM(pUserId,pUserForm);
			
		} catch(Exception ex){
			ex.printStackTrace();
		} finally{
			dm.doDisconnect();
		}
	}
	
	public ArrayList<UserBean> retrieveAllUsers(String pSearchUserName, String pUserMobile,
					String pUserEmail, int pageOffSet){
	
		ArrayList<UserBean> userList = null;
		DBManager dm;
		dm = DBManager.init();
		try{		
			
			dm.doConnect();
			
			YunyshaCM yCM = new YunyshaCM(dm);
			userList = yCM.retrieveAllUsersCM(pSearchUserName, pUserMobile, pUserEmail,pageOffSet);
			
		} catch(Exception ex){
			ex.printStackTrace();
		} finally{
			dm.doDisconnect();
		}
		
		return userList;
	}
	
	public JSONArray retrieveUsersSearch(String pSearchUserName, String pUserMobile,
			String pUserEmail, int pageOffSet){

		JSONArray jArray = new JSONArray();
		DBManager dm;
		dm = DBManager.init();
		try{		
			
			dm.doConnect();
			
			YunyshaCM yCM = new YunyshaCM(dm);
			jArray = yCM.retrieveUsersSearchCM(pSearchUserName, pUserMobile, pUserEmail,pageOffSet);
			
		} catch(Exception ex){
			ex.printStackTrace();
		} finally{
			dm.doDisconnect();
		}
		
		return jArray;
	}

	public JSONArray retrieveData(String selectClause, String table){

		JSONArray jArray = new JSONArray();
		DBManager dm;
		dm = DBManager.init();
		try{		
			
			dm.doConnect();
			
			YunyshaCM yCM = new YunyshaCM(dm);
			jArray = yCM.retrieveData(selectClause,table);
			
		} catch(Exception ex){
			ex.printStackTrace();
		} finally{
			dm.doDisconnect();
		}
		
		return jArray;
	}
	
	public boolean insertOrUpdateUserDetails(UserMasterForm pUserForm){
		
		boolean isSuccess = false;
		DBManager dm;
		dm = DBManager.init();
		try{		
			
			dm.doConnect();
			
			YunyshaCM yCM = new YunyshaCM(dm);
			isSuccess = yCM.insertOrUpdateUserDetailsCM(pUserForm);
			
		} catch(Exception ex){
			ex.printStackTrace();
		} finally{
			dm.doDisconnect();
		}
		
		return isSuccess;
	}
	
public boolean saveCompanyMaster(HashMap<String,String> dataMap){
		
		boolean isSuccess = false;
		DBManager dm;
		dm = DBManager.init();
		try{		
			
			dm.doConnect();
			
			YunyshaCM yCM = new YunyshaCM(dm);
			System.out.println("about to call cm");
			isSuccess = yCM.saveCompanyMaster(dataMap);
			
		} catch(Exception ex){
			ex.printStackTrace();
		} finally{
			dm.doDisconnect();
		}
		
		return isSuccess;
	}
	
public boolean saveDesignMaster(HashMap<String,String> dataMap){
	
	boolean isSuccess = false;
	DBManager dm;
	dm = DBManager.init();
	try{		
		
		dm.doConnect();
		
		YunyshaCM yCM = new YunyshaCM(dm);
		System.out.println("about to call cm");
		isSuccess = yCM.saveDesignMaster(dataMap);
		
	} catch(Exception ex){
		ex.printStackTrace();
	} finally{
		dm.doDisconnect();
	}
	
	return isSuccess;
}

public String getDependentData(String strTableName,String strMasterValue,String strSelectFields,String strWhereFields,String orgID,String strHTMLName)
{
	//LOGGER.info(":::Start Inside OnlineApplicationController getDependentData method:::");   
	final DBManager dm = DBManager.init(); 
	String strDependentDataXML="";
	try
		{	
        dm.doConnect();
		
		YunyshaCM yCM = new YunyshaCM(dm);
		System.out.println("about to call cm");
		
			//strDependentDataXML = yCM.getDependentData(strTableName,strMasterValue,strSelectFields,strWhereFields,strHTMLName);
			
		}
		catch(Exception exc)
		{
			//exc.printStackTrace();
			//LOGGER.error("OnlineApplicationController Exception in getDependentData"+exc.getMessage());
		}
		finally
		{
			try
			{
				//dbManager.doDisconnect();
			}
			catch(Exception exc1)
			{
				// exc1.printStackTrace();
				//LOGGER.error("OnlineApplicationController Exception in getDependentData"+exc1.getMessage());
			}
		}
	 return strDependentDataXML;
}//end of getDependentData()
	
}
